package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.BiConsumer;

@RestController
@RequestMapping("/statics")
@Slf4j
public class staticsController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    DishService dishService;

    @Autowired
    SetmealService setmealService;
    @Autowired
    OrdersService ordersService;

    @RequestMapping("/dishCash")
    public Map<String, List<dishCash>> dishCash() {
        List<Category> cateList = categoryService.list();
        Map map = new HashMap();
        ArrayList<dishCash> dishCashes = new ArrayList<>();
        for (Category category : cateList) {
            Long categoryId = category.getId();
            LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
            qw.eq(Dish::getCategoryId, categoryId);
            List<Dish> list = dishService.list(qw);
            BigDecimal bigDecimal = new BigDecimal("0");
            for (Dish dish : list) {
                LambdaQueryWrapper<OrderDetail> oqw = new LambdaQueryWrapper<>();
                oqw.eq(OrderDetail::getDishId, dish.getId());
                List<OrderDetail> list1 = orderDetailService.list(oqw);
                for (OrderDetail orderDetail : list1) {
                    bigDecimal = bigDecimal.add(orderDetail.getAmount());
                }

            }
            dishCash dishCash = new dishCash();
            dishCash.setValue(bigDecimal);
            dishCash.setName(category.getName());
            dishCashes.add(dishCash);
        }
        map.forEach(new BiConsumer() {
            @Override
            public void accept(Object o, Object o2) {
                System.out.println("-----------" + o.toString());
                System.out.println("-----------" + o2.toString());
            }
        });
        map.put("dishCategoryCash", dishCashes);
        return map;
    }

    @GetMapping("/setmealCash")
    public Map<String, List<dishCash>> setmelCash() {
        //List<Category> cateList = categoryService.list();
        //Map map = new HashMap();
        //ArrayList<SetmealDish> setmealDish = new ArrayList<>();
        //
        //
        //LambdaQueryWrapper<SetmealDish> oqw2 = new LambdaQueryWrapper<>();
        //oqw2.eq(SetmealDish::getDishId,dish.getId());
        //List<SetmealDish> list2 = setmealDishService.list(oqw2);
        //for (SetmealDish setmealDish : list2) {
        //    LambdaQueryWrapper<OrderDetail> oqw1 = new LambdaQueryWrapper<>();
        //    oqw1.eq(OrderDetail::getSetmealId,setmealDish);
        //    List<OrderDetail> list3 = orderDetailService.list(oqw1);
        //    for (OrderDetail orderDetail : list3) {
        //        BigDecimal amount = orderDetail.getAmount();
        //        bigDecimal=bigDecimal.add(amount);
        //    }
        //}
        List<Setmeal> cateList = setmealService.list();
        Map map = new HashMap();
        ArrayList<dishCash> dishCashes = new ArrayList<>();
        for (Setmeal category : cateList) {
            Long categoryId = category.getId();
            LambdaQueryWrapper<OrderDetail> qw = new LambdaQueryWrapper<>();
            qw.eq(OrderDetail::getSetmealId, categoryId);
            List<OrderDetail> list = orderDetailService.list(qw);
            BigDecimal bigDecimal = new BigDecimal("0");
            for (OrderDetail orderDetail : list) {
                BigDecimal amount = orderDetail.getAmount();
                bigDecimal = bigDecimal.add(amount);
            }
            dishCash dishCash = new dishCash();
            dishCash.setValue(bigDecimal);
            dishCash.setName(category.getName());
            dishCashes.add(dishCash);
        }
        map.forEach(new BiConsumer() {
            @Override
            public void accept(Object o, Object o2) {
                System.out.println("-----------" + o.toString());
                System.out.println("-----------" + o2.toString());
            }
        });
        map.put("setmealCash", dishCashes);
        return map;
    }

    @GetMapping("/dishNum")
    public Map<String, List<dishCash>> dishNum() {
        List<Category> cateList = categoryService.list();
        Map map = new HashMap();
        ArrayList<dishCash> dishCashes = new ArrayList<>();
        for (Category category : cateList) {
            Long categoryId = category.getId();
            LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
            qw.eq(Dish::getCategoryId, categoryId);
            List<Dish> list = dishService.list(qw);
            BigDecimal bigDecimal = new BigDecimal("0");
            for (Dish dish : list) {
                LambdaQueryWrapper<OrderDetail> oqw = new LambdaQueryWrapper<>();
                oqw.eq(OrderDetail::getDishId, dish.getId());
                List<OrderDetail> list1 = orderDetailService.list(oqw);
                for (OrderDetail orderDetail : list1) {
                    BigDecimal bigDecimal1 = new BigDecimal("1");
                    bigDecimal = bigDecimal.add(bigDecimal1);
                }

            }
            dishCash dishCash = new dishCash();
            dishCash.setValue(bigDecimal);
            dishCash.setName(category.getName());
            dishCashes.add(dishCash);
        }
        map.forEach(new BiConsumer() {
            @Override
            public void accept(Object o, Object o2) {
                System.out.println("-----------" + o.toString());
                System.out.println("-----------" + o2.toString());
            }
        });
        map.put("dishNum", dishCashes);
        return map;

    }

    @GetMapping("/setmealNum")
    public Map<String, List<dishCash>> setmealNum() {

        List<Setmeal> cateList = setmealService.list();
        Map map = new HashMap();
        ArrayList<dishCash> dishCashes = new ArrayList<>();
        for (Setmeal category : cateList) {
            Long categoryId = category.getId();
            LambdaQueryWrapper<OrderDetail> qw = new LambdaQueryWrapper<>();
            qw.eq(OrderDetail::getSetmealId, categoryId);
            List<OrderDetail> list = orderDetailService.list(qw);
            BigDecimal bigDecimal = new BigDecimal("0");
            for (OrderDetail orderDetail : list) {
                BigDecimal bigDecimal1 = new BigDecimal("1");
                bigDecimal = bigDecimal.add(bigDecimal1);
            }
            dishCash dishCash = new dishCash();
            dishCash.setValue(bigDecimal);
            dishCash.setName(category.getName());
            dishCashes.add(dishCash);
        }
        map.forEach(new BiConsumer() {
            @Override
            public void accept(Object o, Object o2) {
                System.out.println("-----------" + o.toString());
                System.out.println("-----------" + o2.toString());
            }
        });
        map.put("setmealNum", dishCashes);
        return map;
    }

    @GetMapping("/cashPerMonth")
    public Map<String, List> cashPerMonth() {
        Map map = new HashMap();
        ArrayList list1 = new ArrayList();

        list1.add("2021-08");
        list1.add("2021-09");
        list1.add("2021-10");
        list1.add("2021-11");
        list1.add("2021-12");
        list1.add("2022-01");
        list1.add("2022-02");
        list1.add("2022-03");
        list1.add("2022-04");
        list1.add("2022-05");
        list1.add("2022-09");
        list1.add("2022-09");

        ArrayList<BigDecimal> bigDecimals = new ArrayList<>();
        List<Orders> list3 = ordersService.list();

        //for (Orders orders : list3) {
        //    String substring = orders.getOrderTime().toString().substring(0, 7);
        //    BigDecimal bigDecimal = new BigDecimal("0");
        //    for (int i = 0; i < list1.size(); i++) {
        //        if (substring.equals(list1.get(i))) {
        //            BigDecimal amount = orders.getAmount();
        //            bigDecimal.add(amount);
        //        }
        //    }
        //    bigDecimals.add(bigDecimal);
        //}

        for (int i = 0; i < list1.size(); i++) {
            BigDecimal bigDecimal = new BigDecimal("0");
            for (int i1 = 0; i1 < list3.size(); i1++) {
                Orders orders = list3.get(i1);
                String substring = orders.getOrderTime().toString().substring(0, 7);
                System.out.println(list1.get(i));
                if (substring.equals(list1.get(i))) {
                    BigDecimal amount = orders.getAmount();
                   bigDecimal= bigDecimal.add(amount);
                }
            }
            bigDecimals.add(bigDecimal);
        }
        map.put("monthList", list1);
        map.put("dataList", bigDecimals);

        return map;
    }

}